//package com.fly.container.service;
//
//import com.fly.container.model.Blog;
//import com.fly.container.model.Department;
//import org.springframework.stereotype.Service;
//import org.springframework.transaction.TransactionStatus;
//import org.springframework.transaction.annotation.Transactional;
//import org.springframework.transaction.interceptor.TransactionAspectSupport;
//import org.springframework.transaction.support.TransactionCallbackWithoutResult;
//import org.springframework.transaction.support.TransactionTemplate;
//
//import javax.annotation.Resource;
//import java.sql.Timestamp;
//
///**
// * TransactionalService
// *
// * @author Mr.Fu
// * @date 2023/3/19 上午8:57
// */
//@Service
//public class TransactionalService {
//
//    @Resource
//    private BlogService blogService;
//
//    @Resource
//    private DepartmentService departmentService;
//
//    @Resource
//    private TransactionTemplate transactionTemplate;
//
//    /**
//     * 统一事务管理-声明式事务
//     */
//    @Transactional(rollbackFor = Exception.class)
//    public void testTransaction01() {
//        Blog blog = getBlog();
//        blogService.save(blog);
//        if (blog.getUserId().equals(100L)) {
//            throw new RuntimeException("数据异常");
//        }
//        departmentService.save(getDepartment());
//    }
//
//    /**
//     * 事务管理-编程式事务
//     */
//    public void testTransaction02() {
//        Object exceptionResult = transactionTemplate.execute(new TransactionCallbackWithoutResult() {
//            @Override
//            protected void doInTransactionWithoutResult(TransactionStatus transactionStatus) {
//                try {
//                    Blog blog = getBlog();
//                    blogService.save(blog);
//                    if (blog.getUserId().equals(100L)) {
//                        throw new RuntimeException("数据异常");
//                    }
//                } catch (Exception e) {
//                    //回滚
//                    transactionStatus.setRollbackOnly();
//                }
//
//            }
//        });
//        System.out.println("<=================================>" + exceptionResult);
//        departmentService.save(getDepartment());
//    }
//
//    /**
//     * 无事务
//     */
//    public void testTransaction03() {
//        try {
//            Blog blog = getBlog();
//            blogService.save(blog);
//            if (blog.getUserId().equals(100L)) {
//                throw new RuntimeException("数据异常");
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        departmentService.save(getDepartment());
//    }
//
//    /**
//     * 使用
//     * TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
//     * 方式设置回滚->回滚的是整体事务
//     */
//    @Transactional(rollbackFor = Exception.class)
//    public void testTransaction04() {
//        try {
//            Blog blog = getBlog();
//            blogService.save(blog);
//            if (blog.getUserId().equals(100L)) {
//                throw new RuntimeException("数据异常");
//            }
//        } catch (Exception e) {
//            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
//        }
//        departmentService.save(getDepartment());
//    }
//
//    private Blog getBlog() {
//        Blog blog = new Blog();
//        blog.setAddTime(new Timestamp(System.currentTimeMillis()));
//        blog.setUserId(100L);
//        return blog;
//    }
//
//    private Department getDepartment() {
//        Department department = new Department();
//        department.setParentId(200L);
//        department.setDepartmentName("测试部门");
//        department.setLevel(10);
//        return department;
//    }
//}
